import threading
from app.log_config import monitor_logger
from app.udp_server import start_udp_server
from app.heart_check import heart_check


def run_udp_server():
    UDP_SERVER_IP = "127.0.0.1"
    UDP_SERVER_PORT = 8999
    start_udp_server(UDP_SERVER_IP, UDP_SERVER_PORT)


heartCheckInterval = 60
def heart_check_task(stop_event):
    while not stop_event.wait(heartCheckInterval):
        heart_check()

def start():
    monitor_logger.info("start monitor")


    # 创建并启动UDP服务端的线程
    udp_server_thread = threading.Thread(target=run_udp_server)
    udp_server_thread.daemon = True  # 设置为守护线程，当主线程退出时，子线程也会退出
    udp_server_thread.start()

    stop_event = threading.Event()
    heart_check_thread = threading.Thread(target=heart_check_task, args=(stop_event,))
    heart_check_thread.daemon = True
    heart_check_thread.start()

    # thread = threading.Thread(target=monitor)
    # thread.setDaemon(True)
    # thread.start()

    # 使用while True阻塞主进程
    try:
        while True:
            # 主进程在此处被阻塞，但UDP服务端仍然在后台运行
            pass
    except KeyboardInterrupt:
        print("Main process received KeyboardInterrupt, shutting down.")

